var Ticket = {

    selected_row_id:null,
    objGrid:null,

    init:function () {
        var self = this;

        self.objGrid = $('#flex').flexigrid({
            url:$('#table-url').val(),
            reload_after_add: true,
            dataType:'json',
            colModel:[
                {display:'Ticket No.', name:'id', width:60, sortable:true, align:'left', hide:false},
                {display:'Subject', name:'subject', width:130, sortable:true, align:'left', hide:false, editable:{form:'text', validate:'empty', defval:''}},
                {display:'Assign To', name:'user_id', width:130, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:assign_to}},
                {display:'Project', name:'project_id', width:90, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:projects}},
                {display:'Priority', name:'priority_id', width:90, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:priorities}},
                {display:'Status', name:'status_id', width:70, sortable:true, align:'left', hide:false, editable:{form:'select', validate:'', defval:'',options:statuses}},
                {display:'Requested Due Date', name:'due_dt', width:120, sortable:false, align:'left', hide:false, editable:{form:'text', validate:'', defval:'',bclass:'datepicker'}},
                {display:'Estimated Hours', name:'estimated_hrs', width:90, sortable:false, align:'left', hide:false, editable:{form:'text', validate:'', defval:''}}

            ],
            formFields:[
                {type:'buttom', name:'Edit Ticket', bclass:'edit', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Save', bclass:'save', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Cancel', bclass:'cancel', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Track Ticket', bclass:'track', onpress:self.handleGridEvent},
                {separator:true},
                {type:'buttom', name:'Delete Ticket', bclass:'delete', onpress:self.handleGridEvent}
            ],
            searchitems:[
             {display:'Ticket No.', name:'id', isdefault:true}
             ],
            checkboxSelection:true,
            editablegrid:{
                use:true,
                url:$('#table-editable-url').val()
            },
            columnControl:false,
            sortname:"id",
            sortorder:"desc",
            usepager:true,
            useRp:true,
            rp:15,
            showTableToggleBtn:false,
            height:300,
            subgrid:{
                use:false
            }
        });

        $('input.datepicker').live('focus', function(){
            if (false == $(this).hasClass('hasDatepicker')) {
                $(this).mask("9999-99-99");
                $(this).datepicker({
                    inline: true,
                    changeMonth: true,
                    changeYear: true
                });
                $(this).datepicker( "option", "dateFormat", 'yy-mm-dd' );
            }
        });
    },

    handleGridEvent:function (com, grid, json) {
        if (com == 'Save') {
            Ticket.objGrid.flexSaveChanges();
        }
        else if (com == 'Cancel') {
            Ticket.objGrid.flexCancel();
        }
        else if (com == 'Edit Ticket') {

            if (FlexObject.rowSelectedCheck(Ticket.objGrid,grid)) {
                var row = FlexObject.getSelectedRows(grid);
                Ticket.objGrid.flexBeginEdit(row[0]);
            }
        }
        else if(com == 'Track Ticket'){
            if (FlexObject.rowSelectedCheck(Ticket.objGrid,grid,1)) {
                var row = FlexObject.getSelectedRowIds(grid);
                var url = $('#track_ticket_url').val()+'/'+row[0]
                //Ticket.objGrid.flexBeginEdit(row[0]);
                window.open(url, "TrackTicket", "menubar=yes, width=800, height=500,location=no,status=no,scrollbars=yes,resizable=yes");
            }
        }
        else if(com == 'Delete Ticket'){
            if (FlexObject.rowSelectedCheck(Ticket.objGrid,grid)) {
                Ticket.delete_(Ticket.objGrid,grid);
            }
        }
    },

    delete_:function (griObject,grid_dom) {
        var self = this;
        var url = $('#table_delete_url').val();
        Jlib.do_delete(griObject,url, grid_dom);
    }
};

/* when the page is loaded */
$(document).ready(function () {
    Ticket.init();
});